<?php


use app\modules\Admin\models\agentgoodslogModel;
use \app\modules\Merchant\models\goodsModel;

#[代理商品分红记录日志管理]
class AgentgoodslogController extends McommonController
{
    public function init()
    {
        $this->model = new agentgoodslogModel();
        $this->table      = 'agent_goods_log';
        $this->primaryKey = 'goods_id';
        parent::init();
    }

    #[代理商品]
    public function goodslistAction()
    {
        if ($this->isPost()) {
            $query    = goodsModel::query();
            $query    = $query->leftJoin('agent_goods', 'goods.id', '=', 'agent_goods.goods_id');
            array_push($this->search, ['goods.dianpu_id', '=', $this->auth->dianpu_id]);
            $keywords = $this->get('keywords');
            if (!empty($keywords)) {
                array_push($this->search, ['goods.id', '=', $keywords]);
                array_push($this->search, ['goods.title', 'like', "%{$keywords}%", 'or']);
            }
            if (!empty($this->search)) {
                $query = $query->where(function ($query) {
                    foreach ($this->search as $v) {
                        if (isset($v[3]) && strtolower($v[3]) == 'or') {
                            $query = $query->orWhere([$v]);
                        } else {
                            $query = $query->where([$v]);
                        }
                    }
                });
            }

            $total = $query->count();
            $sort  = $this->get('sort', 'sortorder');
            $order = $this->get('order', 'desc');
            $query = $query->orderBy($sort, $order)->orderBy($this->primaryKey, 'DESC');
            $page  = $this->get('page');

            if (!empty($page)) {
                $limit  = $this->get('rows', 10);
                $offset = ($page - 1) * $limit;
                $query  = $query->offset($offset)->limit($limit);
            }
            $rows = $query->select(['agent_goods.*', 'goods.id', 'goods.title', 'goods.price', 'goods.image', 'goods.is_sku'])->get();

            json(['total' => $total, 'rows' => $rows]);
        } else {
            $this->_view->display( strtolower($this->controller) . '/index.html');
        }
    }

    #[商品分红设置日志列表]
    public function goodsloglistAction($returnData = FALSE)
    {
        $goods_id = $this->get('id', 0);
        if($this->isPost()){
            if(!$goods_id){
                ret(4001, '商品参数出错！');
            }
            $this->model = $this->model->where('goods_id', $goods_id);
            return parent::indexAction($returnData); // TODO: Change the autogenerated stub
        }else{
            $goods = goodsModel::query()->where('id', $goods_id)->select(['id', 'title', 'status', 'image', 'price'])->first();
            if($goods){
                $goods = $goods->makeHidden(['dispatch_type_arr', 'parent_category_id'])->toArray();
            }else{
                $goods = [];
            }
            $this->_view->assign('goods', json_encode($goods));
            $this->_view->display( strtolower($this->controller) . '/list.html');
        }
    }

    #[日志详情]
    public function goodsloginfoAction()
    {
        $log_id = $this->get('id', 0);
        $log = agentgoodslogModel::query()->with(['goods'=>function($goodsQuery){
            $goodsQuery->withoutGlobalScope('dianpu_id')->where('dianpu_id', $this->auth->dianpu_id);
        }])->where('id', $log_id)->first();
        if($log){
            $log = $log->toArray();
            if(!empty($log['jc_rules'])){
                $log['jc_rules'] = json_decode($log['jc_rules'], true);
            }
            if(!empty($log['tk_zhitui_rules'])){
                $log['tk_zhitui_rules'] = json_decode($log['tk_zhitui_rules'], true);
            }
            if(!empty($log['tk_jiantui_rules'])){
                $log['tk_jiantui_rules'] = json_decode($log['tk_jiantui_rules'], true);
            }
            if(!empty($log['pj_level1_rules'])){
                $log['pj_level1_rules'] = json_decode($log['pj_level1_rules'], true);
            }
            if(!empty($log['pj_level2_rules'])){
                $log['pj_level2_rules'] = json_decode($log['pj_level2_rules'], true);
            }
        }else{
            $log = [];
        }
        $levelList       = agentlevelModel::get();
        $tuikeLevelList  = tuikelevelModel::get();
        $this->_view->assign('levelList', $levelList->toJson());
        $this->_view->assign('tuikeLevelList', $tuikeLevelList->toJson());
        $this->_view->assign('dataset', json_encode($log));
        $this->_view->display( strtolower($this->controller) . '/info.html');
    }
}